package com.minio.miniospringboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;
import java.io.File;

@Configuration
public class MultipartConfig {

    @Value("${server.upload.path}")
    private String upload;

    /**
     * 文件上传临时路径
     * 注：文件存储临时文件夹必须有，否则上传文件，可能会报错：
     * Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();

        File path = new File(upload);
        // 判断上传文件的保存目录是否存在
        if (!path.exists() || !path.isDirectory()) {
            // 创建目录
            path.mkdirs();
        }
        factory.setLocation(upload);
        return factory.createMultipartConfig();

    }


}
